package microcredit.gui.selector;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.corba.se.spi.orbutil.fsm.ActionBase;

import microcredit.gui.MainFrame;
import microcredit.gui.base.BaseAction;
import microcredit.gui.base.BaseDialog;
import microcredit.gui.util.ColorConstants;
import net.miginfocom.swing.MigLayout;

public abstract class AbstractButtonPanel extends AbstractActionPanel {

	private String title;
	private MToolBar buttonPanel;
	private JPanel mainPanel;
	
	public AbstractButtonPanel() {
		initUI();
		addAncestorListener(new AncestorListener() {
			@Override
			public void ancestorRemoved(AncestorEvent event) {
				applyBackgroundColors();
			}
			
			@Override
			public void ancestorMoved(AncestorEvent event) {
				
			}
			
			@Override
			public void ancestorAdded(AncestorEvent event) {
				applyBackgroundColors();
			}
		});
	}
	
	private void applyBackgroundColors() {
		Color color;
		if (isInMainWindow()) {
			color = ColorConstants.BACKGROUND_MAIN;
		} else if (!isInTabbedPane()) {
			color = ColorConstants.BACKGROUND_DIALOG;
		} else {
			color = ColorConstants.BACKGROUND_TABBED_PANE;
		}
		getButtonPanel().setBackground(color);
		getMainPanel().setBackground(color);
		setBackground(color);
	}
	
	private boolean isInMainWindow() {
		Component parent = this;
		// while not in tabbed pane
		while ( (parent = parent.getParent())  != null) {
			if (parent instanceof MainFrame) {
				return true;
			} else if (parent instanceof Window) {
				break;
			}
		}
		return false;
	}
	
	private boolean isInTabbedPane() {
		Component parent = this;
		// while not in tabbed pane
		while ( (parent = parent.getParent())  != null) {
			if (parent instanceof JTabbedPane) {
				return true;
			}
		}
		return false;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

	public void removeAllActions() {
		super.removeAllActions();
		getButtonPanel().removeAll();
		getButtonPanel().revalidate();
	}
	
	public JLabel addLabel(String text) {
		JLabel label = new JLabel(text);
		getButtonPanel().add(label);
		return label;
	}
	
	public void addAction(BaseAction action) {
		if (action == null) {
			return;
		}
		super.addAction(action);
		getButtonPanel().addAction(action);
	}
	
	public void addExpander() {
		getButtonPanel().addExpander();
	}
	
	protected void initUI() {
		setLayout(new MigLayout("insets 0", "[grow]", "[]0[grow]"));
		add(getButtonPanel(), "cell 0 0,grow");
		add(getMainPanel(), "cell 0 1,grow");
	}
	
	protected MToolBar getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new MToolBar();
		}
		return buttonPanel;
	}
	protected JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
		}
		return mainPanel;
	}
	
	
	public JDialog createModalDialog(Component parent) {
		Window w = SwingUtilities.getWindowAncestor(parent);
		final JDialog dialog = new BaseDialog(w, getTitle(), ModalityType.TOOLKIT_MODAL);
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
			}
			@Override
			public void componentHidden(ComponentEvent e) {
				dialog.dispose();
			}
		});
		dialog.getContentPane().add(this);
		dialog.pack();
		dialog.setLocationRelativeTo(w);
		return dialog;
	}
	
	
	
	
}
